package com.ioandfile;

import org.apache.commons.io.IOUtils;

import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class IOAndMap {
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<>();
        map.put("用户名","密码");
        map.put("yzt","123");
        map.put("sd","255");
        map.put("yzt2222","2222");
        OutputStream os = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;

        try {
            os = new FileOutputStream("/Users/edy/Desktop/file/password.txt");
            osw = new OutputStreamWriter(os);
            bw = new BufferedWriter(osw);
            for (Map.Entry<String, String> entry : map.entrySet()) {
                String str = entry.getKey() + "=" + entry.getValue();
                bw.write(str);
                bw.flush();  //一次性写入
                bw.newLine(); //换行
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(bw);
            IOUtils.closeQuietly(osw);
            IOUtils.closeQuietly(os);
        }


    }
}
